package com.bird.postProcessor;

import com.bird.body.Student;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @data 2023/2/19 17:55
 * @author: bird
 * @description:
 */
@Configuration
public class CustomizedBeanPostProcessor implements BeanPostProcessor {


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName+" 初始化之后执行的方法!");
        if("student-22".equals(beanName)){
            Student student=(Student)bean;
            student.setAge("66");
            student.setName("bird-55");
            return bean;
        }

        return null;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName+": 初始化之前执行的方法!");
        if("student-22".equals(beanName)){
            Student student=(Student)bean;
            student.setAge("12");
            student.setName("bird");
            return bean;
         }
        return null;
    }
    @PostConstruct
    public void init(){
        System.out.println("执行初始化方法");
    }

}
